package com.planesnet.gps1;

import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.app.ProgressDialog;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Gps1Activity extends Activity {

	private FastLocationProvider loc = null;;
	private Button btnLocation;
	private TextView txtLatitud;
	private TextView txtLongitud;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		txtLatitud = (TextView) findViewById(R.id.txtLatitud);
		txtLongitud = (TextView) findViewById(R.id.txtLongitud);
		btnLocation = (Button) findViewById(R.id.btnLocation);

		btnLocation.setText("@string/start_location");

		btnLocation.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				if (loc != null) {
					cancel();
				} else {
					start();
				}
			}
		});

	}

	private void cancel() {
		loc.requestCancel();
		btnLocation.setText("@string/start_location");
		loc = null;
	}

	private void start() {
		loc = new FastLocationProvider(this.getApplicationContext());
		loc.setLocationListener(new AdapterLocation() {
			@Override
			public void onLocationChanged(Location location) {
				updateLocation(location);
			}

		});

		loc.start();
		btnLocation.setText("@string/stop_location");
	}

	private void updateLocation(Location pos) {
		if (pos != null) {
			txtLatitud.setText(pos.getLatitude() + "");
			txtLongitud.setText(pos.getLongitude() + "");
		} else {
			txtLatitud.setText("");
			txtLongitud.setText("");
		}
	}

}